﻿using System.Linq;
using Abp.Authorization;
using Abp.Localization;
using LeadThing.AbpZeroTemplate.Authorization;


namespace LeadThing.AbpZeroTemplate.BasicPersons.Authorization
{
    /// <summary>
    /// 权限配置都在这里。
    /// 给权限默认设置服务
    /// See <see cref="BasicPersonAppPermissions"/> for all permission names.
    /// </summary>
    public class BasicPersonAppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //在这里配置了BasicPerson 的权限。

            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));

            //网格管理
            var entityNameModel = pages.Children.FirstOrDefault(p => p.Name == AppPermissions.Pages_GridManage)
              ?? pages.CreateChildPermission(AppPermissions.Pages_GridManage, L("Pages_GridManage"));

            var basicPerson = entityNameModel.CreateChildPermission(BasicPersonAppPermissions.BasicPerson, L("BasicPerson"));
            basicPerson.CreateChildPermission(BasicPersonAppPermissions.BasicPerson_CreateBasicPerson, L("CreateBasicPerson"));
            basicPerson.CreateChildPermission(BasicPersonAppPermissions.BasicPerson_EditBasicPerson, L("EditBasicPerson"));
            basicPerson.CreateChildPermission(BasicPersonAppPermissions.BasicPerson_DeleteBasicPerson, L("DeleteBasicPerson"));

            //网格员
            var users = entityNameModel.CreateChildPermission(BasicPersonAppPermissions.GridPerson_Users, L("Users"));
            users.CreateChildPermission(BasicPersonAppPermissions.GridPerson_CreatingNewUser, L("CreatingNewUser"));
            users.CreateChildPermission(BasicPersonAppPermissions.GridPerson_EditingUser, L("EditingUser"));
            users.CreateChildPermission(BasicPersonAppPermissions.GridPerson_DeletingUser, L("DeletingUser"));
            users.CreateChildPermission(BasicPersonAppPermissions.GridPerson_ChangingPermissions, L("ChangingPermissions"));
            users.CreateChildPermission(BasicPersonAppPermissions.GridPerson_LoginForUsers, L("LoginForUsers"));

            //注册群众
            var registerPerson = entityNameModel.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_Users, L("Pages_Masses"));
            registerPerson.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_CreatingNewUser, L("CreatingMasses"));
            registerPerson.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_EditingUser, L("EditMasses"));
            registerPerson.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_DeletingUser, L("DeleteMasses"));
            registerPerson.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_ChangingPermissions, L("ChangingPermissions"));
            //registerPerson.CreateChildPermission(BasicPersonAppPermissions.RegisterPerson_LoginForUsers, L("LoginForUsers"));

        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AbpZeroTemplateConsts.LocalizationSourceName);
        }
    }

}